package org.aeon.client.view.components.menuPanel;

import lombok.extern.slf4j.Slf4j;
import org.aeon.client.context.GlobalContext;
import org.aeon.client.view.ApplicationUI;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author: MissingAeon
 * @date: 2024/2/24 15:43
 * @description:
 */
@Slf4j
public class PopupPrompt extends JPanel {
    private static final Deque<Runnable> popupPrompts = new ArrayDeque<>();
    private static final AtomicBoolean complete = new AtomicBoolean(true);
    private static ApplicationUI mainUI;


    private PopupPrompt(String content) {
        if (mainUI == null) {
            mainUI = GlobalContext.getContext().getMainUI();
        }

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                mainUI.removePopUpLayer(PopupPrompt.this);
                complete.set(true);
            }
        });

        this.setSize(300, 40);

        this.setBounds(mainUI.getWidth() - this.getWidth() - 20, 5, 300, 40);
        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        final Border border = BorderFactory.createMatteBorder(1, 1, 1, 1, new Color(0x887DFFF6, true));
        this.setBorder(border);
        this.setBackground(new Color(0x88FFFFFF, true));

        final Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalGlue());
        JLabel label = new JLabel(content);
        label.setFont(new Font("黑体", Font.PLAIN, 14));
        label.setAlignmentX(Component.CENTER_ALIGNMENT);
        box.add(label);
        box.add(Box.createHorizontalGlue());
        this.add(box);


    }

    /**
     * 用于弹出提示条
     * 任意线程可调用
     *
     * @param content
     */
    public static void createPopUp(String content) {
        SwingUtilities.invokeLater(() -> {
            if (GlobalContext.getContext().getMainUI() != null) {
                final PopupPrompt popupPrompt = new PopupPrompt(content);
                popupPrompts.addFirst(() ->
                        {
                            try {
                                SwingUtilities.invokeAndWait(() -> {
                                    mainUI.addPopUpLayer(popupPrompt, -1);
                                    final Timer timer = new Timer(2000, e -> {
                                        mainUI.removePopUpLayer(popupPrompt);
                                        complete.set(true);
                                    });
                                    timer.setRepeats(false);
                                    timer.start();
                                });
                            } catch (InterruptedException | InvocationTargetException e) {
                                log.error(e.getMessage(), e);
                            }
                        }
                );
                // 相同频率输出提示框
                GlobalContext.getContext().getSingleThreadExecutor().execute(() -> {
                    while (!popupPrompts.isEmpty()) {
                        if (complete.getAndSet(false)) {
                            popupPrompts.removeLast().run();
                        }
                    }
                });
            }
        });
    }

}
